package com.bootdo.system.controller;

import com.bootdo.system.config.MD5Utils;
import com.bootdo.system.domain.ApptupianDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.service.ApptupianService;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PersonController extends BaseController {

    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private ApptupianService apptupianService;


    @GetMapping("/person/setdianshuPage")
    public String personsetdianshuPage(Long userid, HttpServletRequest request) {

        FrontuserDO user = frontuserService.get(userid);
        request.setAttribute("user", user);

        return "setdianshuPage";
    }

    @ResponseBody
    @PostMapping("/person/updatedianshu")
    public R updatedianshu(FrontuserDO frontuserDO, HttpServletRequest request) {

        FrontuserDO userDO = getUserDO(request);
        if(frontuserDO.getDianshu().compareTo(userDO.getDianshu())>0){
            return R.error("点数超过了父节点，设置失败");
        }

        frontuserDO.setParentid(userDO.getUserid());
        frontuserService.updateByUserIdAndpId(frontuserDO);

        return R.ok();
    }

    @GetMapping("/fenxiang")
    public String fenxiang(HttpServletRequest request) {

        ApptupianDO fenxiang = apptupianService.get(6);

        request.setAttribute("fenxiang", fenxiang);

        FrontuserDO userDO = getUserDO(request);

        request.setAttribute("userDO", userDO);

        return "fenxiang";
    }

    @GetMapping("/person")
    public String personPage(HttpServletRequest request) {

        FrontuserDO userDO = getUserDO(request);

        request.setAttribute("userDO", userDO);

        request.setAttribute("active", "5");
        return "person";
    }


    @GetMapping("/personinfo")
    public String personinfoPage(HttpServletRequest request) {

        FrontuserDO userDO = getUserDO(request);

        request.setAttribute("userDO", userDO);


        return "personinfo";
    }

    @PostMapping("/person/update")
    public String updatePerson(FrontuserDO frontuserDO, HttpSession session, HttpServletRequest request) {
        //登录密码
        if (StringUtils.isNotBlank(frontuserDO.getLoginpass())) {
            String encrypt = MD5Utils.encrypt(frontuserDO.getUsername(), frontuserDO.getLoginpass());
            frontuserDO.setLoginpass(encrypt);
        }else{
            frontuserDO.setLoginpass(null);
        }

        //支付密码
        if (StringUtils.isNotBlank(frontuserDO.getPaypass())) {
            String encrypt = MD5Utils.encrypt(frontuserDO.getUsername(), frontuserDO.getPaypass());
            frontuserDO.setPaypass(encrypt);
        }else{
            frontuserDO.setPaypass(null);
        }
        FrontuserDO userDO = getUserDO(request);
        //手机验证码不可以为空
        if(StringUtils.isBlank(frontuserDO.getPhonecode())||StringUtils.isBlank(frontuserDO.getPhone())){
            request.setAttribute("editpersoninfotip", "手机验证码为空");
            request.setAttribute("userDO", userDO);
            return "personinfo";
        }

        //手机验证码
        if (StringUtils.isNotBlank(frontuserDO.getPhone())) {
            Object phone = session.getAttribute("phone");
            System.out.println("修改资料验证码="+phone);
            if (phone == null || !phone.toString().equals(frontuserDO.getPhonecode())) {
                request.setAttribute("editpersoninfotip", "手机验证码校验失败");
                request.setAttribute("userDO", userDO);
                return "personinfo";
            }
            //验证码失效
            session.removeAttribute("phone");
        }
        //防止接口漏洞


        frontuserDO.setYouxiaotime(userDO.getYouxiaotime());
        frontuserDO.setDianshu(userDO.getDianshu());
        frontuserDO.setZichan(userDO.getZichan());

        frontuserService.update(frontuserDO);

        return "redirect:/person";
    }
}
